import {io} from 'socket.io-client'
import storage from "@/utils/storage";

export default class Socket {
    client
    channel = 'c1'

    constructor() {
        if (!this.client) {
            this.connect()
        }

    }

    connect() {
        let userInfo = storage.get('userInfo')
        userInfo = userInfo?JSON.parse(userInfo) :{}
        const url = `ws://127.0.0.1:7070?user_id=${userInfo.id}`
        this.client = io(url, {transports: ["websocket"]});
        console.log(this.client)
        this.client.on('connect', () => {
            console.log('connect!!!!!!!!');
        });
    }
    on(n,c) {
        this.client.on(n, (res) => {
            c && c(res)
        })
    }
    emit({m, a, c}) {
        const m_u = `controller.${m}`
        this.client.emit(this.channel, {cmd: m_u, params: a}, (res) => {
            c && c(res)
        })
    }
}